﻿package com.ztgame.modules.sound
{
	import flash.events.*;
	import flash.media.*;
	
	//	import org.allencuilib.console.Logger;
	
	public class PlayableSoundObject extends EventDispatcher
	{		
		public static const SOUNT_TYPE_EFFECT:int = 1;
		public static const SOUNT_TYPE_BG:int = 2;
		
		public var mName:String;
		public var mType:int;
		public var mChannel:SoundChannel;
		public var channelArr:Vector.<SoundChannel>;
		public var mVolume:Number=1;
		private var mSound:Sound;
		public var mPosition:Number=0;
		public var mStartTime:Number=0;
		public var mLoops:int=0;
		public var mPaused:Boolean=false;
		public var mLoaded:Boolean=false;
		
		public function PlayableSoundObject()
		{
			channelArr = new Vector.<SoundChannel>();
		}
		
		public function getSound() : Sound
		{
			return this.mSound;
		}
		
		public function setSound(sound:Sound) : void
		{
			this.mSound = sound;
			if (this.mSound.bytesLoaded == this.mSound.bytesTotal)
			{
				this.mLoaded = true;
				dispatchEvent(new Event(Event.COMPLETE));
			}
			else
			{
				this.mSound.addEventListener(Event.COMPLETE, this.loaded);
				this.mSound.addEventListener(IOErrorEvent.IO_ERROR, this.error);
				this.mSound.addEventListener(IOErrorEvent.NETWORK_ERROR, this.error);
			}
		}
		
		//同一声音，可以播放多次
		public function play(startTime:Number = 0, loops:int = 0):void
		{
			try
			{
			this.mStartTime = startTime;
			this.mLoops = loops;			
			
			if(mSound)
			{
				var c:SoundChannel = mSound.play(mStartTime, mLoops);
				if(mVolume != 1 && c)
				{
					c.soundTransform = new SoundTransform(mVolume);
				}
				
				if(channelArr.length > 40)
				{
					channelArr.splice(0,20);
				}
				channelArr.push(c);
				mChannel = c;
				mPaused = false;
			}
			}
			catch(e:Error)
			{
				trace(e.toString());
			}
		}
		
		//同一个声音，只播放一次
		public function playSingle(startTime:Number = 0, loops:int = 0):void
		{
			try
			{
			this.mStartTime = startTime;
			this.mLoops = loops;
			
			if(mChannel)
			{
				mChannel.stop();
			}
			
			if(mSound)
			{
				mChannel = mSound.play(mStartTime, mLoops);
				if(mVolume != 1 && mChannel)
				{
					mChannel.soundTransform = new SoundTransform(mVolume);
				}
				mPaused = false;
			}
			}
			catch(e:Error)
			{
				trace(e.toString());
			}
		}
		
		public function pause():void
		{			
			mPaused = true;
			
			if(mChannel)
			{
				mPosition = int(mChannel.position);
				mChannel.stop();				
			}
			
			for each(var s:SoundChannel in channelArr)
			{
				s.stop();
			}
		}
		
		public function goOn():void
		{
			if(mPaused && mSound)
			{
				for each(var s:SoundChannel in channelArr)
				{
					s = mSound.play(0, mLoops);
					if(mVolume != 1 && s)
					{
						s.soundTransform = new SoundTransform(mVolume);
					}
				}
				
				mChannel = mSound.play(mPosition, mLoops);
				if(mVolume != 1 && mChannel)
				{
					mChannel.soundTransform = new SoundTransform(mVolume);
				}
				mPaused = false;
			}
		}
		
		public function stop():void
		{
			mPaused = true;
			
			if(mChannel)
			{
				mChannel.stop();
				mPosition = 0;				
			}
			
			for each(var s:SoundChannel in channelArr)
			{
				if(s)
				s.stop();
			}
			channelArr.length = 0;			
		}
		
		private function error(event:Event) : void
		{
			this.mSound.removeEventListener(Event.COMPLETE, this.loaded);
			this.mSound.removeEventListener(IOErrorEvent.IO_ERROR, this.error);
			this.mSound.removeEventListener(IOErrorEvent.NETWORK_ERROR, this.error);
			this.mLoaded = true;
			dispatchEvent(new Event(ErrorEvent.ERROR));
			//Logger.Warning(this, "load sound err " + mName);
		}
		
		private function loaded(event:Event) : void
		{
			this.mSound.removeEventListener(Event.COMPLETE, this.loaded);
			this.mSound.removeEventListener(IOErrorEvent.IO_ERROR, this.error);
			this.mSound.removeEventListener(IOErrorEvent.NETWORK_ERROR, this.error);
			this.mLoaded = true;
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
	}
}
